package top.infopub.mgr.mkt.web;

import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.domain.BidCurrencyDomain;
import top.infopub.mgr.mkt.service.BidPlanService;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.mgr.mkt.vo.BidCurrencyVo;
import top.infopub.model.mkt.BidPlan;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

/**
 * @Author Warking
 * @Date 2021/4/26 9:29
 * @Version 1.0
 */
@Controller
@RequestMapping("/bidPlan")
public class BidPlanController extends GenericFlowController {
    @Autowired
    private BidPlanService bidPlanService;

    @Autowired
    private ProProcessManageService proProcessManageService;
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<BidCurrencyDomain> queryDataByCondition(BidCurrencyVo bidCurrencyVo, ParamPage paramPage) throws ParseException {
        Page<BidCurrencyDomain> page = new Page<BidCurrencyDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = bidPlanService.selectPage(page, bidCurrencyVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询标书策划详情  > 注意：方法命名必须为：toDealOrDetail
     *
     * @param proCode 标书策划proCode
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public String queryById(HttpServletRequest request, String proCode, String id) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("proCode", proCode);
        request.setAttribute("id", id);
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");
        BidPlan bidPlan = bidPlanService.selectByPrimaryKey(id);
        if (bidPlan != null) {
            if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), bidPlan.getProcessStatus())
                    || StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), bidPlan.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (StringUtils.isNotBlank(bidPlan.getProcessId()) && bidPlan.getProcessId().length() > 10) {
                request.setAttribute("processId", bidPlan.getProcessId());
                String auditTabFlag = proProcessManageService.checkShowAuditTab(bidPlan.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), bidPlan.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        return "mkt/lay-tenderPlanning-detail";
    }

    //审批
    @RequestMapping("/auditProcess")
    @ResponseBody
    public RspVo auditProcess(String id, String processId, String status, String auditMsg) {
        //立项审批
        proProcessManageService.auditProject(id, processId, status, auditMsg, null);
        return RspVo.getSuccessResponse();
    }
}
